package cn.tedu.mybatis;

import cn.tedu.dao.CarMapper;
import cn.tedu.pojo.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestCar {
    SqlSessionFactory factory;
    @Before
    public void init() throws IOException {
        //读取核心配置文件
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");

        //创建会话工厂
        factory = new SqlSessionFactoryBuilder().build(in);
    }
    @Test
    public void get(){
        //打开sql会话
        SqlSession session = factory.openSession();

        CarMapper car = session.getMapper(CarMapper.class);
        List<Car> list = car.selectList();

        for (Car c : list){
            System.out.println(c);
        }

    }
    @Test
    public void show(){
        SqlSession session = factory.openSession();
        Car car = session.selectOne("cn.tedu.dao.CarMapper.selectCar");

        System.out.println(car);
    }
}
